/**
 * 游戏结束界面
 */
class GameOverUI extends UIBase{
    // private box_Gp:eui.Group;
    private btn_home:BaseButton;
    // private btn_box:BaseButton;
    private rank_group:eui.Group;
    private btn_show:BaseButton;
    private btn_again:BaseButton;
    private isCanShow = true;
    private recommendItem:RecommendUI;
    constructor(data?)
    {
        super('GameOverView', data);
    }

    public init()
    {
        super.init(true);
        this.initAnimation();
        this.requsetIsExperienceWeapon();
        // RankManager.instance().updateFriendCloudStorage();
        this.reviewHide();
        RankManager.instance().showNearlyFriendRank();
        this.getRank();
        // if(!RoleCache.instance().reviewStatus)
        // {
        //     this.btn_home.alpha = 0;
        //     egret.setTimeout(()=>{
        //         egret.Tween.get(this.btn_home).to({
        //             alpha:1
        //         }, 500);
        //     }, this, 1500);
        // }
    }

    private reviewHide()
    {
        if(RoleCache.instance().reviewStatus)
        {
            this.btn_show.visible = false;
        }
    }

    private initAnimation()
    {
        // EffectUtil.instance().shakeEffect(this.btn_box);
    }

    public getRank()
    {
        let bitmap_rank = WeChatgGameManager.instance().createRankListDisplayObject();
        if(bitmap_rank)
        this.rank_group.addChild(bitmap_rank);
    }

    public addEvent()
    {
        this.btn_show.addTapHandler(this.showHandle, this);
        // this.btn_box.addTapHandler(this.jumpToOther,this);
        this.btn_home.addTapHandler(this.homeHandle,this);
        this.btn_again.addTapHandler(this.playAgain, this);
        GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    public playAgain()
    {
        let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        if(logic)
        {
            logic.playAgain();
            this.remove(4);
        }
    }

    public homeHandle()
    {
        ScreenMovies.MovieStart(1,()=>{
             let da = {
                index:0
            }
            UIManager.instance().openUILogic(MainUI,da);
        },this);
    }

    public showHandle()
    {
        if(!this.isCanShow)
        {
            MessageUtil.tips('只能炫耀一次哦！');
            return
        }
        let curCustoms = GameManager.instance.curCustoms;
        let title = `我已经来到${curCustoms}关了，你敢来挑战我？`;
        SharePrase.instance().requestOneAdvertisement({type:ShareId.show,title:title});
    }

    protected shareSuccess(e:BaseEvent)
    {
        let type = e.param.type;
        if(type == ShareId.show)
        {
            let parma = {
                type:'share_award'
            }
            ActivityPrase.instance().getWelfare(parma, this.reset, this);
        }
    }

    public reset()
    {
        this.isCanShow = false;
    }

    public removeEvent()
    {
        // this.btn_box.removeTapHandler();
        this.btn_show.removeTapHandler();
        this.btn_home.removeTapHandler();
        this.btn_again.removeTapHandler();
        GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    private jumpToOther()
    {
        let data = {
            isPop:true,
            showBG:true
        }
        UIManager.instance().openUILogic(BoxUI2,data);
    }

    /**
     * 请求是否体验武器
     */
    private requsetIsExperienceWeapon()
    {
        let data = {
            version:GameConst.version
        }
        let self = this;
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorweapon/experience_weapon' , StringUtil.jsonToHttpPostString(data), (e)=>{
            let da = JSON.parse(e);
            if(da && da.msg == 'ok')
            {
                let weaponId = da.data;
                //可以体验
                let data = {
                    isPop:true,
                    effect:1,
                    weaponId:weaponId
                }
                UIManager.instance().openUILogic(LuckWeaponUI, data);
            }
        })
    }

    public clear()
    {
        super.clear();
        this.isCanShow = null;
        this.recommendItem.remove();
        RankManager.instance().closeFriendCloudStorage();
    }
}